<%@ page import="org.apache.commons.logging.*" %>

<%@ page import="com.tobesoft.xplatform.data.*" %>
<%@ page import="com.tobesoft.xplatform.tx.*" %>

<%@ page import = "java.util.*" %>
<%@ page import = "java.sql.*" %>
<%@ page import = "java.io.*" %>

<%@ page contentType="text/xml; charset=UTF-8" %>

<%! 
/*********** 공통함수 *************/
// DataSet 값 가져오기, 단, "null"을 ""로
public String  dsGet(DataSet ds, int rowno, String colid) throws Exception
{
	String value;
	value = ds.getString(rowno,colid);
	if( value == null )
		return "";
	else
		return value;
} 
%>

<%
// PlatformData 생성
PlatformData o_xpData = new PlatformData();
	
int nErrorCode = 0;
String strErrorMsg = "START";

// HttpServletRequest를 이용하여 HttpPlatformRequest 생성
HttpPlatformRequest pReq = new HttpPlatformRequest(request);
	
// XML 데이터 분석
pReq.receiveData();
// 데이터를 PlatformData 형태로 저장
PlatformData i_xpData = pReq.getData();

// PlatformData로부터 List 획득 및 Dataset, 변수 추출
VariableList in_vl = i_xpData.getVariableList();

String in_var1 = request.getParameter("in_var1");
String in_var2 = in_vl.getString("in_var2");

DataSet ds = i_xpData.getDataSet("in_ds");

try {	
	/******* JDBC Connection *******/
	Connection conn = null;
	Statement  stmt = null;
	ResultSet  rs   = null;
	
	try {
		//Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
		//conn = DriverManager.getConnection("jdbc:sqlserver://61.107.23.159:1433;DatabaseName=EDU;User=edu;Password=edu123");
		
		Class.forName("com.mysql.jdbc.Driver");
		conn = DriverManager.getConnection("jdbc:mysql://localhost/jal?autoReconnect=true&relaxAutoCommit=true", "root", "root");
    
		stmt = conn.createStatement();
	
		String SQL = "";
		int	i;
		/******** Dataset을 INSERT, UPDATE처리 ********/
		for(i=0;i<ds.getRowCount();i++)
		{
			int rowType = ds.getRowType(i);
			if( rowType == DataSet.ROW_TYPE_INSERTED )
			{
				SQL = "insert into tb_bnkrup ( BILL_CODE, CENTER_CODE, RUPT_DATE, TAKEOVER, STORE ) values ( " +
							"'" + dsGet(ds,i,"BILL_CODE")   + "'," + 
							"'" + dsGet(ds,i,"CENTER_CODE") + "'," + 
							"'" + dsGet(ds,i,"RUPT_DATE")   + "'," +  
							"'" + dsGet(ds,i,"TAKEOVER")    + "'," +
							"'" + dsGet(ds,i,"STORE")       + "')";
			}
			else if( rowType == DataSet.ROW_TYPE_UPDATED )
			{
				String org_bill_code = ds.getSavedData(i,"BILL_CODE").toString(); 
				SQL = "update tb_bnkrup set " +
							 "BILL_CODE   = '" + dsGet(ds,i,"BILL_CODE")   + "'," + 
							 "CENTER_CODE = '" + dsGet(ds,i,"CENTER_CODE") + "'," +
							 "RUPT_DATE   = '" + dsGet(ds,i,"RUPT_DATE")   + "'," +  
							 "TAKEOVER    = '" + dsGet(ds,i,"TAKEOVER")    + "'," +
							 "STORE       = '" + dsGet(ds,i,"STORE")       + "' " + 
							 "where BILL_CODE = " + "'" + org_bill_code + "'";
			}
			//rs = stmt.executeQuery(SQL);
			stmt.executeUpdate(SQL);		
		}
		/****** Dataset을 DELETE처리 ****/
		for( i = 0 ; i< ds.getRemovedRowCount() ; i++ )
		{
			String bill_code = ds.getRemovedData(i,"BILL_CODE").toString();
			SQL = "delete from tb_bnkrup where " +
					  "BILL_CODE = " + "'" + bill_code + "'";
			//rs = stmt.executeQuery(SQL);
			stmt.executeUpdate(SQL);
		}
		
		conn.commit();
		
		// VariableList에 값을 직접 추가
		nErrorCode = 0;
	  strErrorMsg = "SUCC";

	} catch (SQLException e) {
	  conn.rollback();
		nErrorCode = -1;
		strErrorMsg = e.getMessage();
	}	
	
	/******** JDBC Close ********/
	if ( stmt != null ) try { stmt.close(); } catch (Exception e) {nErrorCode = -1; strErrorMsg = e.getMessage();}
	if ( conn != null ) try { conn.close(); } catch (Exception e) {nErrorCode = -1; strErrorMsg = e.getMessage();}
			
} catch (Throwable th) {
	nErrorCode = -1;
	strErrorMsg = th.getMessage();
}

// VariableList 참조
VariableList varList = o_xpData.getVariableList();
		
// VariableList에 값을 직접 추가
varList.add("ErrorCode", nErrorCode);
varList.add("ErrorMsg", strErrorMsg);

// HttpServletResponse를 이용하여 HttpPlatformResponse 생성
HttpPlatformResponse pRes = new HttpPlatformResponse(response, PlatformType.CONTENT_TYPE_XML, "UTF-8");
pRes.setData(o_xpData);

// 데이터 송신
pRes.sendData();
%>
